import org.w3c.dom.css.Counter;

/*
* synchronized的使用方法
* synchronized不仅可以用来修饰代码块,还可以用来修饰实例方法和静态方法
* */


class counter{
    public static int count;

    synchronized public void increase(){
        /*
        * 相当于使用了this作为锁对象
        * */
        count++;
    }

    public void increase2(){
        synchronized(this){
            count++;
        }
    }

    synchronized public static void increase3(){
        /*
        * 相当于是用了类对象作为锁对象
        * */
        count++;
    }

    public static void increase4(){
        synchronized (counter.class){
            count++;
        }
    }


}
public class Demo11 {
    public static void main(String[] args) throws InterruptedException {
        counter counter = new counter();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(counter.count);
    }
}
